<?php

$action = getAction();

switch ($action) {
	case "index":
		$user = $db->getRow("select * from " . table("user") . " where openid = '$openid'");
		
		$smarty->assign("user", $user);
		$smarty->display("sign.html");
		break;
	case "in":
		$userId = $db->getOne("select id from " . table("user") . " where openid = '$openid'");
		$date = date("Y-m-d");
		$result = array();
		
		$check = $db->getOne("select count(1) from " . table("point_log") . " where user_id = '$userId' and point_type = '" . POINT_TYPE::SIGN . "'");
		
		if($check) {
			$result["errmsg"] = "您今日已签到,不能重复签到!";
		} else {
			$point = rand($setting["sign_point_min"], $setting["sign_point_max"]);
			$data["user_id"] = $userId;
			$data["point"]   = $point;
			$data["point_type"] = POINT_TYPE::SIGN;
			$data["point_date"] = $date;
			$data["insert_time"] = date("Y-m-d H:i:s");
			
			$db->insert("point_log", $data);
			$db->query("update " . table("user") . " set point = point + $point, point_amount = point_amount + $point where id = '$userId'" );
			
			$result["point"] = $point;
		}
		
		echo json_encode($result);
		exit();
		
		break;
}
?>